package com.qckj.amz.controller;

import com.qckj.amz.domain.Shop;
import com.qckj.amz.service.IShopService;
import com.qckj.common.annotation.Log;
import com.qckj.common.core.controller.BaseController;
import com.qckj.common.core.domain.AjaxResult;
import com.qckj.common.core.page.TableDataInfo;
import com.qckj.common.enums.BusinessType;
import com.qckj.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 店铺维护Controller
 *
 * @author luchuan
 * @since 1.0 2021-11-07
 */
@RestController
@RequestMapping("/amz/shop")
public class ShopController extends BaseController {
    @Autowired
    private IShopService shopService;

    /**
     * 查询店铺维护列表
     */
    @PreAuthorize("@ss.hasPermi('amz:shop:list')")
    @GetMapping("/list")
    public TableDataInfo list(Shop shop) {
        Page<Shop> page = shopService.findShopPage(shop);
        return getDataTable(page);
      /*  TableDataInfo data= shopService.getShopPage(shop);
        return data;*/

    }

    /**
     * 导出店铺维护列表
     */
    @PreAuthorize("@ss.hasPermi('amz:shop:export')")
    @Log(title = "店铺维护", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Shop shop) {
        List<Shop> list = shopService.findShopList(shop);
        ExcelUtil<Shop> util = new ExcelUtil<>(Shop.class);
        return util.exportExcel(list, "shop");
    }

    /**
     * 获取店铺维护详细信息
     */
    @PreAuthorize("@ss.hasPermi('amz:shop:query')")
    @GetMapping(value = "/{shopId}")
    public AjaxResult getInfo(@PathVariable("shopId") Long shopId) {
        return AjaxResult.success(shopService.findById(shopId));
    }

    /**
     * 新增店铺维护
     */
    @PreAuthorize("@ss.hasPermi('amz:shop:add')")
    @Log(title = "店铺维护", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Shop shop) {
        shopService.save(shop);
        return AjaxResult.success();
    }

    /**
     * 修改店铺维护
     */
    @PreAuthorize("@ss.hasPermi('amz:shop:edit')")
    @Log(title = "店铺维护", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Shop shop) {
        shopService.edit(shop);
        return AjaxResult.success();
    }

    /**
     * 删除店铺维护
     */
    @PreAuthorize("@ss.hasPermi('amz:shop:remove')")
    @Log(title = "店铺维护", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shopIds}")
    public AjaxResult remove(@PathVariable Long[] shopIds) {
        shopService.deleteByIds(Arrays.asList(shopIds));
        return AjaxResult.success();
    }

    /**
     * 通过用户username获取可绑定的店铺
     * @return
     */
    @RequestMapping("/getShopByUsername")
    @ResponseBody
    public AjaxResult getShopByUsername(@RequestBody String userName) {
        AjaxResult ajaxResult = shopService.getShopByUsername(userName);
        return ajaxResult;
    }


}
